package com.project.service.dao.impl;

import com.project.service.bean.UserBean;
import com.project.service.dao.IUserDao;

import java.sql.SQLException;
import java.time.LocalDate;
import java.util.ArrayList;
import java.util.List;

/**
 * 用户持久接口实现类
 */
public class UserDaoImpl extends BaseDao implements IUserDao {
    @Override
    public void add(UserBean user) {
        this.setConnection();

        try {
            ps = con.prepareStatement("insert into t_user(u_name,u_password,u_birthday,u_rank) values(?,?,?,?)");
            ps.setObject(1,user.getName());
            ps.setObject(2,user.getPwd());
            ps.setObject(3,user.getBirthday());
            ps.setObject(4,user.getLevel());

            ps.executeUpdate();
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }finally{
            this.closeConnection();
        }
    }

    @Override
    public List<UserBean> findByBirthday(LocalDate startDate, LocalDate endDate) {
        List<UserBean> list = new ArrayList<>();
        this.setConnection();

        try {
            ps = con.prepareStatement("select * from t_user where u_birthday>=? and u_birthday<=?");
            ps.setObject(1,startDate);
            ps.setObject(2,endDate);

            rs = ps.executeQuery();
            while(rs.next()){
                list.add(this.getUser());
            }

        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }finally{
            this.closeConnection();
        }

        return list;
    }

    @Override
    public List<UserBean> findByItem(String name, LocalDate startDate, LocalDate endDate) {
        List<UserBean> userList = new ArrayList<>();
        String sql = "select * from t_user where 1=1 ";
        List valueList = new ArrayList();
        if(name != null && name.length() != 0){
            sql += "and u_name like ? ";
            valueList.add("%"+name+"%");
        }
        if(startDate != null){
            sql += "and u_birthday >= ? ";
            valueList.add(startDate);
        }
        if(endDate != null){
            sql += "and u_birthday <= ? ";
            valueList.add(endDate);
        }

        this.setConnection();
        try {
            ps =con .prepareStatement(sql);
            for(int i=0;i<valueList.size();i++){
                ps.setObject(i+1,valueList.get(i));
            }
            rs = ps.executeQuery();
            while(rs.next()){
                userList.add(this.getUser());
            }

        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }

        return userList;
    }

    private UserBean getUser() throws SQLException {
        UserBean user = new UserBean();
        user.setId(rs.getInt("pk_userId"));
        user.setName(rs.getString("u_name"));
        user.setPwd(rs.getString("u_password"));
        user.setBirthday(LocalDate.parse(rs.getString("u_birthday")));
        user.setLevel(rs.getString("u_rank"));
        return user;
    }
}
